<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { LoginApi } from '@/api/user'
const router=useRouter()
const resetForm=()=>{
  loginForm.value={
    username:'',
    password:''
  }
}
const login=async()=>{
 const result= await LoginApi(loginForm.value)
  if(result.code){
    ElMessage.success("恭喜登陆成功")
    localStorage.setItem('loginUser',JSON.stringify(result.data))

    router.push('/')
  }else{
 
    ElMessage.error('登录失败')
  }
}


const loginForm=ref({
    username:'',
    password:''
})
const rules = ref({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 50, message: '用户名的长度咋3到50之间', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 3, max: 50, message: '密码的长度咋3到50之间', trigger: 'blur' }
]
})

</script>

<template>
  <div class="login-container">
    <el-form
    ref="ruleFormRef"
    style="max-width: 700px"
    :model="loginForm"
    :rules="rules"
    label-width="auto"
  >
    <el-form-item label="请输入用户名" prop="username">
      <el-input v-model="loginForm.username" />
    </el-form-item>
    <el-form-item label="请输入密码" prop="password">
      <el-input v-model="loginForm.password" />
    </el-form-item>
    <el-form-item>
       <el-button type="success" @click="login">登录</el-button>
       <el-button type="danger" @click="resetForm">重置</el-button>
    </el-form-item>
    </el-form>
  </div>
</template>

<style>
.login-container{
  min-height:100vh;
  flex-direction: column;
  justify-content: center;
  align-items: right;
  display: flex;
  background-image:url("../../assets/senlin.png");
  background-position: 0%;
  border-radius: 2%;
  background-size: cover;
}

</style>



